import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './common/http-exception.filter';
import { ValidationPipe } from './common/validation.pipe';
import { XmlMiddleware } from './common/xml.middleware';
import { AuthGuard } from './common/auth.guard';
import { ResponseInterceptor } from './common/response.interceptor';
import { SwaggerConfig } from './config/swagger.config';
import * as session from 'express-session';
import * as cors from 'cors';
import { customArray } from './utils/array.prototype';
import {NestExpressApplication} from '@nestjs/platform-express'
import { join } from 'path';
// import { RolesGuard } from './common/roles.guard';
// 将自定义方法应用到所有的数组对象上
Object.defineProperty(Array.prototype, 'DateFormatMap', { value: customArray.prototype.DateFormatMap });
async function bootstrap() {
  const app= await NestFactory.create<NestExpressApplication>(AppModule);
  app.setGlobalPrefix('admin');
  app.useStaticAssets(
    join(__dirname, 'uploads'),
    {
      prefix: '/images'
    }
  )
  // 注册全局session中间件
  app.use(session({ secret: 'ztf',name: 'app.session', rolling: true, }))
  // 注册跨域处理
  app.use(cors())
  // 注册全局xml支持中间件
  app.use(new XmlMiddleware().use)
  // 注册全局身份验证守卫
  app.useGlobalGuards(new AuthGuard())
  // 注册全局角色验证守卫
  // app.useGlobalGuards(new RolesGuard())
  // 注册全局响应拦截器
  app.useGlobalInterceptors(new ResponseInterceptor())
  // 注册全局dto验证管道
  app.useGlobalPipes(new ValidationPipe())
  // 注册全局异常过滤器
  app.useGlobalFilters(new HttpExceptionFilter())
  // 注册swagger在线文档
  SwaggerConfig.register(app)
  await app.listen(3000);
}
bootstrap();
